package com.wkck.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wkck.VO.AdminLoginVo;
import com.wkck.constant.EditPowerConstant;
import com.wkck.constant.MessageConstant;
import com.wkck.context.BaseContext;
import com.wkck.domain.Admin;
import com.wkck.dto.AdminEditPowerDto;
import com.wkck.dto.PasswordDto;
import com.wkck.exception.AdminNotFoundException;
import com.wkck.exception.AdminPasswordError;
import com.wkck.exception.EditorException;
import com.wkck.exception.LockAdminException;
import com.wkck.mapper.AdminMapper;
import com.wkck.service.AdminService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;

import java.nio.charset.StandardCharsets;
import java.util.Objects;

@Service
@Slf4j
public class AdminServiceImpl extends ServiceImpl<AdminMapper, Admin> implements AdminService {

    @Autowired
    private AdminMapper adminMapper;

    @Autowired
    private RedisTemplate<Object,Object> redisTemplate;

    /**
     * 管理员登录
     */
    @Override
    public Admin adminLogin(Admin adminDTO) {
        // 管理员账号
        String admin = adminDTO.getAdmin();
        // 管理员密码
        String password = adminDTO.getPassword();

        Admin queryByAdmin = adminMapper.getByAdmin(admin);

        if (Objects.isNull(queryByAdmin)) {
            throw new AdminNotFoundException(MessageConstant.ADMIN_NOT_FOUND);
        }

        if (queryByAdmin.getDel_flag().equals(EditPowerConstant.CLOSE)) {
            throw new LockAdminException(MessageConstant.LOCK_ADMIN);
        }

        password = DigestUtils.md5DigestAsHex(password.getBytes(StandardCharsets.UTF_8));

        if (!password.equals(queryByAdmin.getPassword())) {
            throw new AdminPasswordError(MessageConstant.PASSWORD_ERROR);
        }

        log.info("管理员信息： {}",queryByAdmin);
        return queryByAdmin;
    }

    /**
     * 校验管理员密码
     * @param passwordDto
     * @return
     */
    @Override
    public Boolean validPassword(PasswordDto passwordDto) {
        String password = passwordDto.getPassword();

        Admin byAdmin = adminMapper.getByAdmin(passwordDto.getAdmin());

        password = DigestUtils.md5DigestAsHex(password.getBytes(StandardCharsets.UTF_8));

        return byAdmin.getPassword().equals(password);
    }

    /***
     * 修改管理员信息
     * @param adminDto
     * @return
     */
    @Override
    @Transactional
    public AdminLoginVo edit(Admin adminDto) {

        adminDto.setPassword(DigestUtils.md5DigestAsHex(adminDto.getPassword().getBytes(StandardCharsets.UTF_8)));

        LambdaUpdateWrapper<Admin> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(Admin::getId,adminDto.getId());
        updateWrapper.eq(Admin::getAdmin,adminDto.getAdmin());

        updateWrapper.set(Admin::getPassword,adminDto.getPassword());
        updateWrapper.set(Admin::getPhone,adminDto.getPhone());
        updateWrapper.set(Admin::getEmail,adminDto.getEmail());
        updateWrapper.set(Admin::getIntroduction,adminDto.getIntroduction());

        this.update(updateWrapper);

        Admin admin = adminMapper.getByAdmin(adminDto.getAdmin());
        AdminLoginVo adminLoginVo = new AdminLoginVo();

        BeanUtils.copyProperties(admin,adminLoginVo);

        return adminLoginVo;
    }

    /***
     * 修改管理员权限
     * @param adminEditPowerDto
     * @return
     */
    @Override
    @Transactional
    public Boolean editPower(AdminEditPowerDto adminEditPowerDto) {
        Long currentId = BaseContext.getCurrentId();
        Admin admin = this.getById(currentId);

        if ( !admin.getAdmin().equals("admin") ) {
            throw new EditorException(MessageConstant.EDITOR_ERROR);
        }

        LambdaUpdateWrapper<Admin> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(Admin::getId,adminEditPowerDto.getId());

        String power = adminEditPowerDto.getDel_flag().equals(EditPowerConstant.OPEN) ? EditPowerConstant.OPEN : EditPowerConstant.CLOSE;

        updateWrapper.set(Admin::getDel_flag,power);

        return this.update(updateWrapper);
    }
}
